
function parseBrand(brandData = {}, userData = {}) {
    let userJob = userData.job || {};
    let userRoles = userData.adminRoles || [];
    let result = {};
    try {
        let brandModules = brandData.brandModules || [];
        result = brandModules.reduce((res, item) => {
            let {subModuleConfigList, moduleRuleConfigList} = item;
            let obj = {
                moduleType: item.moduleType,
                moduleName: item.moduleName
            };
            parseSubModule(obj, subModuleConfigList, userJob, userRoles);
            parseModuleRule(obj, moduleRuleConfigList, userJob, userRoles);
            res[item.moduleKey] = obj;
            return res;
        }, {});
    } catch (err) {
        console.error('parseBrand = ', err);
    }
    return result;
}

function parseSubModule(parent, moudles, userJob, userRoles) {
    if(typeof parent === 'object' && Array.isArray(moudles)) {
        moudles.forEach(item => {
            let obj = {
                moduleType: item.moduleType,
                moduleName: item.moduleName
            };
            if(item.subModuleConfigList) {
                parseSubModule(obj, item.subModuleConfigList, userJob, userRoles);
            }
            parent[item.moduleKey] = 
            parseModuleRule(obj, item.moduleRuleConfigList, userJob, userRoles);
        })
    }
    return parent;
}

function parseModuleRule(parent, moudles, userJob, userRoles) {
    if(typeof parent === 'object' && Array.isArray(moudles)) {
        for(let item of moudles) {
            let moduleRuleKey = item.moduleRuleKey;
            let temp = (item.ruleOnOffList || []).find(i => {
                return i.onOffKey == 0 && i.checked; // 检测是否是不限定范围，0表示不限，1表示限定，checked表示选中
            });
            parent[moduleRuleKey] = {
                moduleRuleName: item.moduleRuleName
            };
            let rule;
            let ruleList = item.ruleList || [];
            for(let r of ruleList) {
                rule = {
                    ruleKey: r.ruleKey,
                    ruleName: r.ruleName,
                    checked: !!temp
                };
                
                if(moduleRuleKey == 'AREA_SCOPE' || moduleRuleKey == 'LOGIN_SUPPORT_OUTSIDE_SCOPE') {
                    let arr = [];
                    r.subRules.forEach(item => {
                        arr.push({
                            'ruleKey': item.ruleKey,
                            'ruleName': item.ruleName,
                            'checked': (temp && temp.checked) || item.checked
                        });
                    });
                    rule.subRules = arr;
                    delete rule.checked;
                } else {
                    let subRule = r.subRules.find(sr => {
                        return (sr.ruleKey == userJob.code && sr.checked) || (userRoles.indexOf(sr.ruleKey) != -1 && sr.checked);
                    });
                    if(subRule) {
                        subRule.checked = (temp && temp.checked) || subRule.checked;
                        rule.subRuleKey = subRule.ruleKey;
                        rule.subRuleName = subRule.ruleName;
                        rule.checked = subRule.checked;
                    }
                }
                
                // if(moduleRuleKey == 'AREA_SCOPE') {
                //     rule.checked = true;
                // }
                parent[moduleRuleKey][rule.ruleKey] = rule;
            }
            if(ruleList.length == 0) {
                // 不限的话，就是为true
                parent[moduleRuleKey] = !!temp;
                /*
                if(temp) {
                    parent[moduleRuleKey] = !!temp;
                } else {
                    // 找出限制为true的,只取第一个为true的，目前只支持单选的情况
                    let other = (item.ruleOnOffList || []).find(i => {
                        return i.onOffKey != 0 && i.checked;
                    });
                    parent[moduleRuleKey] = !!other;
                }
                */
            }
        }
    }
    return parent;
}

// 递归遍历品牌key
function fmtBrandPageKey(list = [], pageMap = {}) {
    if(list.length) {
        list.forEach(item => {
            if (item.checked) {
                pageMap[item.moduleKey] = {
                    moduleKey: item.moduleKey,
                    moduleName: item.moduleName,
                    moduleRuleList: item.moduleRuleList
                }
            }
            if(item.checked && item.children && item.children.length) {
                fmtBrandPageKey(item.children, pageMap);
            }
        })
    }
    return pageMap;
}


export {
    parseBrand,
    fmtBrandPageKey
};
